interface Config {
  fontSize?: number; //字体大小
  fontColor?: string;
  padding?: number;
  alpha?: number;
  rotate?: number;
  background?: string;
}

export function textBecomeImg(textT: string, configT?: Config) {
  const text = textT ? textT : ' ';
  const config = configT ? configT : {};
  const {
    fontSize = 14,
    fontColor = '#666',
    alpha = 0.5,
    rotate = 30,
    padding = 80,
    background = 'transparent',
  } = config;
  const canvas = document.createElement('canvas');
  const fwidth = fontSize * text.length + 30;
  const space = fwidth + padding * 2;
  canvas.height = space;
  canvas.width = space;
  const context: any = canvas.getContext('2d');
  const center = space / 2;
  context.translate(center, center);
  context.rotate((rotate * Math.PI) / 180);

  context.fillStyle = background;
  context.fillRect(-100, -100, 200, 200);

  context.globalAlpha = alpha;
  context.fillStyle = fontColor;
  context.textAlign = 'center';
  context.textBaseline = 'middle';
  context.font = fontSize + 'px Arial';
  context.fillText(text, 0, 0);
  const dataUrl = canvas.toDataURL('image/png'); //注意这里背景透明的话，需要使用png
  return dataUrl;
}
